<?php
include 'db.php';
$boardId = $_REQUEST["boardId"]; // 餐桌ID
$dishId = $_REQUEST["dishId"]; // 菜品ID
$name = $_REQUEST['name'];
if (isset($boardId) && isset($dishId)) {

    // 订单ID的获取： 根据 餐桌ID 查询 未结账的订单
    $sql = "select * from dish_order where board_id=$boardId and `status`='未结账'";
    $sqls = $conn->prepare($sql);
    $ok = $sqls->execute();
    $order = $sqls->fetch();
    $order_id = $order['id'];

    // 菜品单价的获取： 根据 菜品ID 查询 指定菜品
    $sql = "select * from dish where id=$dishId";
    $sqlp = $conn->prepare($sql);
    $ok = $sqlp->execute();
    $dish = $sqlp->fetch();
    $price = $dish['price'];

    // 将菜品添加到订单  菜品ID 订单ID 菜品单价
    $sql = "insert into order_item(dish_id,order_id,price) values($dishId,$order_id,$price)";
    $sqlp = $conn->prepare($sql);
    $ok = $sqlp->execute();

    $sql = "update dish_order set money=money+$price where id=$order_id";
    $sqlo = $conn->prepare($sql);
    $ok = $sqlo->execute();

    // update dish_order set money=money+$price where id=$order_id;


    // 调整到 dc.php
    header("Location:./dc.php?boardId=" . $boardId);
}





//---------------------------
//$id = $_REQUEST['id'];
//
//if (empty($id)) {
//    exit('没有选餐桌');
//}
//
//include_once './db.php';
//$sql = "select * from board where id=$id;";
//$sqls = $conn->prepare($sql);
//$ok = $sqls->execute();
//if ($ok) {
//    $board = $sqls->fetch();
//} else {
//    echo '数据暂时不可用';
//}
